package com.gbs.info2.tools;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import android.content.Context;
import com.gbs.info2.R;

public class ConfigFileHelper {
	private Context context;

	public ConfigFileHelper(Context context) {
		this.context = context;
	}

	public void writeLine(int lineCount, String line) {
		ArrayList<String> lines = new ArrayList<String>();
		InputStream is = getInputStream();
		if (is != null) {
			InputStreamReader isr = new InputStreamReader(is);
			BufferedReader bis = new BufferedReader(isr);
			try {
				String strLine;
				while ((strLine = bis.readLine()) != null) {
					lines.add(strLine);
				}
				is.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			lines.set(lineCount - 1, line);
			createAppDatFile(lines);
		}
	}

	public String readLine(int lineCount) {
		String result = "";
		InputStream is = getInputStream();
		if (is != null) {
			InputStreamReader isr = new InputStreamReader(is);
			BufferedReader bis = new BufferedReader(isr);
			try {
				for (int i = 0; i < lineCount; i++) {
					result = bis.readLine();
				}
				is.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return result;
	}

	public boolean isConfigFileExist() {
		try {
			context.openFileInput(context.getString(R.string.dateFileName));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			return false;
		}
		return true;
	}

	private InputStream getInputStream() {

		InputStream is = null;
		try {
			is = context
					.openFileInput(context.getString(R.string.dateFileName));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			// osetruje prve spustenie, tvorba default udajov
			createAppDatFile(null);
			try {
				is = context.openFileInput(context
						.getString(R.string.dateFileName));
			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			// e.printStackTrace();
		}
		return is;
	}

	public void createAppDatFile(ArrayList<String> lines) {
		try {
			OutputStreamWriter osw = new OutputStreamWriter(
					context.openFileOutput(
							context.getString(R.string.dateFileName), 1));
			// ak sa riadky null - vytvara sa default subor
			if (lines == null) {
				osw.write("xxx");
				osw.write("\n");
				osw.write(context.getString(R.string.defaultServerUrl));
				osw.write("\n");
				osw.write(context.getString(R.string.defaultAutoUpdate));
			} else {
				for (int i = 0; i < lines.size(); i++) {
					osw.write(lines.get(i));
					if (i != (lines.size() - 1)) {
						osw.write("\n");
					}
				}
			}
			osw.flush();
			osw.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public boolean deleteConfigFile (){

		File configFile = new File("/data/data/com.gbs.info2/files/"+ context.getString(R.string.dateFileName));
		boolean result = configFile.delete();
		return result;
	}

	public String getMd5Digest(String input) {
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] messageDigest = md.digest(input.getBytes());
			BigInteger number = new BigInteger(1, messageDigest);
			return number.toString(16);
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException(e);
		}
	}
}
